package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectStatusDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectStatus;

import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class GraduateProjectStatusService {
    private static GraduateProjectStatusDao graduateProjectStatusDao =
            GraduateProjectStatusDao.getInstance();
    private static GraduateProjectStatusService graduateProjectStatusService =
            new GraduateProjectStatusService();

    public static GraduateProjectStatusService getInstance() {
        return graduateProjectStatusService;
    }

    public GraduateProjectStatus find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        GraduateProjectStatus result = graduateProjectStatusDao.find(id, connection);
        JdbcHelper.close(connection);
        return result;
    }

    public Collection<GraduateProjectStatus> findAll()
            throws SQLException {

        Connection connection = JdbcHelper.getConn();
        Collection<GraduateProjectStatus> graduateProjectStatuses =
                graduateProjectStatusDao.findAll(connection);
        JdbcHelper.close(connection);
        return graduateProjectStatuses;
    }

    public boolean update(GraduateProjectStatus graduateProjectStatus) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            updated = graduateProjectStatusDao.update(graduateProjectStatus, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(GraduateProjectStatus graduateProjectStatus) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean added = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            added = graduateProjectStatusDao.add(graduateProjectStatus, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return added;
    }

    public boolean delete(Integer id) throws SQLException, BysjException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        GraduateProjectStatus graduateProjectStatus = this.find(id);
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            deleted = graduateProjectStatusDao.delete(graduateProjectStatus, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public boolean delete(GraduateProjectStatus graduateProjectStatus, Connection connection)
            throws SQLException, BysjException {
        boolean result = false;
        int countTeacherOfProfTitle = CommonDao.getInstance().
                countWhereForeignKey(connection, "graduateProject",
                        "graduateProjectStatus_id", graduateProjectStatus.getId());
        //若没有二级单位，则能够删除
        if (countTeacherOfProfTitle == 0) {
            result = graduateProjectStatusDao.delete(graduateProjectStatus, connection);
        } else {
            throw new BysjException(graduateProjectStatus.getDescription() + "有下属系，不能删除");
        }
        JdbcHelper.close(connection);
        return result;
    }
}
